package com.wpw.web.work.invoice;

import com.alibaba.fastjson.JSONObject;
import com.wpw.web.util.HttpUtil;
import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @ClassName: InvoiceUrlTestClass
 * @CreateDate: 2020-06-12 17:27
 * @author: wupengwei
 */
public class InvoiceUrlTestClass {

    //http://edianzu-pms.oss-cn-beijing.aliyuncs.com/invoice/history/
    public static void main(String[] args) throws IOException {
        List<String> invoiceList = getInvoiceFromFile();

        List<String> errorList = new ArrayList<>();

        String jsFile = "/Users/wupengwei/IdeaProjects/webtest/src/main/java/com/wpw/web/work/invoice/noinvoice.txt";
        Path jsPath = Paths.get(jsFile);
        BufferedWriter writer = Files.newBufferedWriter(jsPath, StandardCharsets.UTF_8);

        int i = 0;
        for (String invoiceNo : invoiceList) {
            i ++;
            if (i % 100 == 0) {
                System.out.println("第" + i + "个");
            }
            String data = checkStatus(invoiceNo);
            if (StringUtils.isNotBlank(data)) {
                writer.write(data);
                writer.newLine();
                System.out.println(data);
                errorList.add(data);
            }
        }
        writer.flush();


    }

    public static List<String> getInvoiceFromFile() {

        List<String> invoiceNos = new ArrayList<>();


        String filePath = "/Users/wupengwei/IdeaProjects/webtest/src/main/java/com/wpw/web/work/invoice/invoice.txt";
        Path path = Paths.get(filePath);
        try {
            List<String> codes = Files.readAllLines(path, StandardCharsets.UTF_8);
            int totalSize = codes.size();
            System.out.println("共" + totalSize + "条数据");

            for (String code : codes) {
                invoiceNos.add(code);
            }



        } catch (IOException e) {
            e.printStackTrace();
        }

        return invoiceNos;

    }

    public static String checkStatus(String invoiceNo) {
        Map<String, Object> param = new HashMap<>();
        String url = "w" + invoiceNo + ".jpg";
        try {
            URL u = new URL(url);
            HttpURLConnection uConnection = (HttpURLConnection) u.openConnection();
            uConnection.setReadTimeout(3000);
            uConnection.setConnectTimeout(3000);
            try {
                uConnection.connect();
                int responseCode = uConnection.getResponseCode();
                if (responseCode == 404) {
                    uConnection.disconnect();
                    return invoiceNo;
                }
                uConnection.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
           // e.printStackTrace();
        }
        return null;
    }
}